import $Element from "@/core/Element";
import { DOMcreateElement } from "@/core/jsxFactory";
import ToolBar from "./toolBar";

export default class RichEditor extends $Element {
    private container: $Element;
    private toolBarElement: ToolBar;
    constructor(bodyContainer: HTMLElement) {
        super(<div></div>);

        this.container = new $Element(bodyContainer);
        this.addClassName("editor");
        this.attr("contenteditable", "true");
        this.toolBarElement = new ToolBar(bodyContainer);
        this.container.appendChild(this);
    }
    cc() {
        const selection = document.getSelection();
        if (selection && (selection.focusNode?.parentNode == this.current || selection.focusNode?.parentNode?.parentNode == this.current)) {
            const range = selection.getRangeAt(0);
            const targetNode = range.commonAncestorContainer;
            let text = range.commonAncestorContainer.nodeValue?.substring(range.startOffset, range.endOffset);
            var newNode = document.createElement('span');
            newNode.className = "bold-text";
            range.surroundContents(newNode);
        }
    }

}